package com.aaa.sys.service.impl;

import com.aaa.common.entity.Role;
import com.aaa.common.entity.User;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.ResultStatus;
import com.aaa.common.util.TokenUtil;
import com.aaa.common.vo.Page;
import com.aaa.sys.dao.RoleDao;
import com.aaa.sys.dao.UserDao;
import com.aaa.sys.service.RoleService;
import com.aaa.sys.service.UserRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 角色信息表(Role)表服务实现类
 *
 * @author makejava
 * @since 2025-09-15 10:13:02
 */
@Service("roleService")
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleDao roleDao;
    @Resource
    private UserDao userDao;
    @Resource
    private HttpServletRequest request;
    @Resource
    private UserRoleService userRoleService;

    @Override
    public List<Role> queryRoleByUserId(Integer userId) {
        return roleDao.queryRoleByUserId(userId);
    }

    /**
     * 根据角色ID查询菜单权限
     *
     * @param roleId
     * @return
     */
    @Override
    public List<String> queryMenuByRoleId(Integer roleId) {
        return roleDao.queryMenuByRoleId(roleId);
    }

    /**
     * 查询角色权限标识
     *
     * @param userId
     * @return
     */
    @Override
    public List<String> queryRoleKey(Integer userId) {
        return roleDao.queryRoleKey(userId);
    }


    /**
     * 查询所有角色
     *
     * @return java.util.List<com.aaa.common.entity.Role>
     **/
    @Override
    public List<Role> selectAll() {
        return roleDao.selectAll();
    }

    /**
     * 通过ID查询单条数据
     *
     * @param roleId 主键
     * @return 实例对象
     */
    @Override
    public Role queryById(Integer roleId) {
        return this.roleDao.queryById(roleId);
    }

    /**
     * 分页查询
     *
     * @param page 筛选条件
     * @return 查询结果
     */
    @Override
    public PageInfo queryByPage(Page<Role> page) {
        //分页操作  分页参数一定要进行传递
        if (page == null || page.getPageNo() == null || page.getPageNo() == 0 || page.getPageSize() == null || page.getPageSize() == 0) {
            //进行业务编写时，可以抛出自定义异常
            throw new CustomException(ResultStatus.PARAM_NOT_EMPTY.getReturnCode(),
                    ResultStatus.PARAM_NOT_EMPTY.getReturnMessage());
        }
        PageHelper.startPage(page.getPageNo(), page.getPageSize());
        //获取查询对象
        Role role = page.getData();
        return new PageInfo(roleDao.queryAllByLimit(role));
    }

    /**
     * 新增数据
     *
     * @param role 实例对象
     * @return 实例对象
     */
    @Override
    public Role insert(Role role) {
        String userId = TokenUtil.getUserId(request);
        User user = userDao.queryById(Integer.parseInt(userId));
        role.setCreateBy(user.getUserName());
        role.setUpdateBy(user.getUserName());
        role.setDelFlag("0");
        role.setStatus("0");
        this.roleDao.insert(role);

        return role;
    }

    /**
     * 修改数据
     *
     * @param role 实例对象
     * @return 实例对象
     */
    @Override
    public Role update(Role role) {
        String userId = TokenUtil.getUserId(request);
        User user = userDao.queryById(Integer.parseInt(userId));
        role.setUpdateBy(user.getUserName());
        this.roleDao.update(role);
        return this.queryById(role.getRoleId());
    }

    /**
     * 通过主键删除数据
     *
     * @param roleId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer roleId) {
        return this.roleDao.deleteById(roleId) > 0;
    }
}
